﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class Shippers : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack==false)
        {
            initialAOOD();
            FillShiipper();
            ViewState["SeComp"]=false;
        }
       
    }
    void initialAOOD()
    {
        con = new SqlConnection();
        con.ConnectionString = WebConfigurationManager.ConnectionStrings["SqlStr"].ConnectionString;
        cmd = new SqlCommand();
        cmd.Connection = con;
    }
    void FillShiipper()
    {
        initialAOOD();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_getShipper";
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable("Shipper");
        da.Fill(dt);
        
        gvShipper.AutoGenerateSelectButton = true;
        gvShipper.DataSource = dt;
        gvShipper.DataBind();
    }
    void addnewShipper()
    {
        try
        {

            initialAOOD();
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_InsertShipper";
            SqlParameter para = new SqlParameter("@companyname", SqlDbType.NVarChar);
            para.Size = 40;
            para.Value = txtCompanyName.Text.Trim();
            cmd.Parameters.Add(para);
            para = new SqlParameter("@phonenumber", SqlDbType.NVarChar);
            para.Size = 24;
            para.Value = txtPhone.Text.Trim();
            cmd.Parameters.Add(para);
            cmd.ExecuteNonQuery();
            string script = "alert(\"Insert successfull!\");";
            ScriptManager.RegisterStartupScript(this, GetType(),
                                  "ServerControlScript", script, true); ;
        }
        catch (Exception ex)
        {
            string script = string.Format("alert(\"{0}\");", ex.Message);
            ScriptManager.RegisterStartupScript(this, GetType(),
                                  "ServerControlScript", script, true); ;
        }
        finally
        {

            con.Close();
        }
    }
    void updateShipper()
    {
        try
        {
            initialAOOD();
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_UpdateShipper";
            SqlParameter para = new SqlParameter("@shipperID", SqlDbType.Int);
            para.Value = txtID.Text;
            cmd.Parameters.Add(para);
            para = new SqlParameter("@companyname", SqlDbType.NVarChar);
            para.Size = 40;
            para.Value = txtCompanyName.Text;
            cmd.Parameters.Add(para);
            para = new SqlParameter("@phonenumber", SqlDbType.NVarChar);
            para.Size = 24;
            para.Value = txtPhone.Text;
            cmd.Parameters.Add(para);
            cmd.ExecuteNonQuery();
            gvShipper.DataBind();
            string script = "alert(\"Update successfull!\");";
            ScriptManager.RegisterStartupScript(this, GetType(),
                                  "ServerControlScript", script, true); ;
        }
        catch (Exception ex)
        {
            string script = string.Format("alert(\"{0}\");", ex.Message);
            ScriptManager.RegisterStartupScript(this, GetType(),
                                  "ServerControlScript", script, true); ;
        }
        con.Close();
    }
    void clearfield()
    {
        txtID.Text = "";
        txtCompanyName.Text = "";
        txtPhone.Text = "";
        txtSearch.Text = "";
        

    }
    void deleteShipper()
    {
        try
        {
            initialAOOD();
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_DeleteShipper";
            SqlParameter para = new SqlParameter("@shipperID", SqlDbType.Int);
            para.Value = txtID.Text;
            cmd.Parameters.Add(para);
            cmd.ExecuteNonQuery();
            gvShipper.DataBind();
            string script = "alert(\"DELETE successfull!\");";
            ScriptManager.RegisterStartupScript(this, GetType(),
                                  "ServerControlScript", script, true); ;
        }
        catch (Exception ex)
        {

            string script = string.Format("alert(\"{0}\");", ex.Message);
            ScriptManager.RegisterStartupScript(this, GetType(),
                                  "ServerControlScript", script, true); ;
        }
        con.Close();
    }
    void searchID()
    {
        try
        {
            initialAOOD();
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_SearchShipper";
            SqlParameter para = new SqlParameter("@shipperID", SqlDbType.Int);
            para.Value = txtSearch.Text;
            cmd.Parameters.Add(para);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("seID");
            da.Fill(dt);
            gvShipper.DataSource = dt;
            gvShipper.DataBind();
        }
        catch (Exception )
        {
            string script = string.Format("alert(\"YOu must ID as a number\");");
            ScriptManager.RegisterStartupScript(this, GetType(),
                                  "ServerControlScript", script, true); ;
        }
        finally
        {
            con.Close();
        }
    }
    void searchname()
    {
        try
        {
            ViewState["SeComp"] = true;
            initialAOOD();
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_SearchShipperCompName";
            SqlParameter para = new SqlParameter("@CompName", SqlDbType.NVarChar);
            para.Size = 40;
            para.Value = txtSearch.Text.Trim();
            cmd.Parameters.Add(para);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("seName");
            da.Fill(dt);
            gvShipper.DataSource = dt;
            gvShipper.DataBind();
        }
        catch (Exception ex)
        {
            string script = string.Format("alert(\"{0}\");", ex.Message);
            ScriptManager.RegisterStartupScript(this, GetType(),
                                  "ServerControlScript", script, true); ;
        }
        finally
        {
            con.Close();
        }
    }
    protected void gvShipper_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        gvShipper.PageIndex = e.NewPageIndex;
        gvShipper.DataBind();
        if ((bool)ViewState["SeComp"]==true)
        {
            searchname();
        }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        clearfield();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        updateShipper();

    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        addnewShipper();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        deleteShipper();
    }
    protected void btnReload_Click(object sender, EventArgs e)
    {
        FillShiipper();
    }
    protected void btnsearch_Click(object sender, EventArgs e)
    {
        searchname();
    }
    protected void gvShipper_SelectedIndexChanged(object sender, EventArgs e)
    {
        
            GridViewRow r = gvShipper.SelectedRow;
            txtID.Text = Server.HtmlDecode(r.Cells[1].Text);
            txtCompanyName.Text = Server.HtmlDecode(r.Cells[2].Text);
            txtPhone.Text = Server.HtmlDecode(r.Cells[3].Text);
      
    }
}
